//
// @Auther: by wujehy 
// @Email:wujehy@qq.com
// @Data:2020/9/12
// @Time:9:35 上午
//
//

#ifndef GEEJOAN_CLIENTSERVICE_H
#define GEEJOAN_CLIENTSERVICE_H

#include <deque>
#include "boost/asio/ip/tcp.hpp"
#include <ThreadPool.h>
#include "ClientModManager.h"

namespace GeeJoan
{

    class ClientService
    {
        boost::asio::io_context &io_context;
        boost::asio::ip::tcp::socket client;

        uint8_t taskid = 0;

        void do_connect(const boost::asio::ip::tcp::resolver::results_type &endpoints);

        void do_operations();


        void do_read(boost::system::error_code &ec);

        void do_write();

        boost::asio::const_buffer write_buffer_;
        std::array<char, NETWORKPACKAGE_MAX_BUFFER_LEN+1> data_;
        std::deque<std::string> write_deque;

        void do_reconnect();

        ThreadPool threadPool;
        ClientModManager modManager;
        int id;
        std::string serveHost;
        std::string port;
    public:
        std::function<void()> connect_success_callback;

        int clientid();

        void setServeHost(const std::string &serveHost);

        void setPort(const std::string &port);

        void init();

        int send(uint8_t modType, uint8_t subtype, const std::string &msg, std::function<void(int status)> callback = nullptr);

        void close();

        void reset();

        ClientService(boost::asio::io_context &io_context_, int threadSize = 2);

        bool registerModFunc(BaseClientMod *mod, bool auto_del = true);

        virtual ~ClientService();

    };

}
#endif //GEEJOAN_CLIENTSERVICE_H
